var OutToOrder = function () {
  var template = /*html*/ `
    <div class="outToOrderDiv" v-loading="loading">
      <div style="margin-bottom:5px;">
        <el-button size="medium" type="primary" @click="isShowToOrderForm=true">订单查询条件</el-button>
      </div>
      <div class="outToOrderDiv-header" v-if="type===1&&user!==null">
        <span>领料人信息 : </span>
        <span>部门 : {{user.deptName}}</span>
        <span>车间 : {{user.workShopName}}</span>
        <span>人员 : {{user.userName}}</span>
      </div>
      <div class="outToOrderDiv-header">
        <span>物料代码:{{toOrderMaterialData.materialCode}}</span>
        <span>供应商:{{toOrderMaterialData.providerName}}</span>
        <span>物料名称:{{toOrderMaterialData.materialName}}</span>
        <span>编号:{{toOrderMaterialData.color}}</span>
        <span>描述:{{toOrderMaterialData.colorDescription}}</span>
        <span>库存数/单位:{{toOrderMaterialData.warehouseCount}} {{toOrderMaterialData.unit}}</span>
        <span>规格:{{toOrderMaterialData.modal}}</span>
        <span v-if="toOrderMaterialData.unit !=='付' && toOrderMaterialData.productionUnitCount>0">
          库存数/单位:{{toOrderMaterialData.productionUnitCount}}付
        </span>
      </div>
      <div class="outToOrderDiv-table-div">
        <el-table :data="orderInfoDetailList" fit border style="width:100%;" height="parent">
          <el-table-column prop="orderNo" label="系统订单编号" min-width="140px" header-align="center">
          </el-table-column>
          <el-table-column prop="companyOrderId" label="订单编号" min-width="140px" header-align="center">
          </el-table-column>
          <el-table-column prop="factoryNo" label="工厂型号" min-width="120px" header-align="center">
          </el-table-column>
          <el-table-column prop="customerNo" label="客户型号" min-width="120px" header-align="center">
          </el-table-column>
          <el-table-column prop="lineNo" label="订单序号" width="75px" align="center">
            <template slot-scope="scope">
              <span>{{scope.row.lineNo==='-1'?0:scope.row.lineNo}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="materialType" label="物料类型" min-width="120px" header-align="center">
          </el-table-column>
          <el-table-column prop="materialCode" label="物料代码" min-width="140px" header-align="center">
          </el-table-column>
          <el-table-column prop="color" label="编号" min-width="120px" header-align="center"></el-table-column>
          <el-table-column prop="colorDescription" label="描述" min-width="120px" header-align="center">
          </el-table-column>
          <el-table-column prop="materialModel" label="规格" min-width="120px" header-align="center">
          </el-table-column>
          <el-table-column prop="feedTotal" label="订单投料数" min-width="80px" align="center">
          </el-table-column>
          <el-table-column :resizable="false" v-if="toOrderMaterialData.unit !=='付'" label="物料投料数" :auto-width="true"
            width="130px" header-align="center">
            <template slot-scope="props">
              <el-input-number :controls="false" v-model="props.row.feedCount" :min="0" :precision="0" style="width:120px" size="mini">
              </el-input-number>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="出库数量" width="130px" header-align="center">
            <template slot-scope="props">
              <el-input-number v-if="toOrderMaterialData.warehouseCount>0" :controls="false" v-model="props.row.count"
                :min="0" :precision="0" style="width:120px" size="mini"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" v-if="toOrderMaterialData.unit !=='付'" label="交接数(付)" width="100px" header-align="center">
            <template slot-scope="props">
              <el-input-number v-if="props.row.unit!=='付'" :controls="false" v-model="props.row.selfOutFuCount" :min="0"
                :precision="0" style="width:90px" size="mini"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="操作" width="70px" align="center">
            <template slot-scope="props">
              <el-button type="primary" size="mini" @click="saveOut(props.row)">出库</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div style="margin-top:10px;text-align:right;" v-if="pagination.totalCount!==0">
        <el-pagination background :page-size="pagination.pageSize" :total="pagination.totalCount"
          :current-page.sync="pagination.currentPage" @current-change="pageChange">
        </el-pagination>
      </div>
      <!-- 查询条件dialog -->
      <el-dialog title="物料查询条件" append-to-body :visible.sync="isShowToOrderForm" width="100%" top="0px" :show-close="false">
        <sys-common-search request-url="materialSelectFilter/getMaterialSelectData" :request-data="{type:14}"
          :search="search" :search-field="['companyOrderId','orderId','factoryNo','customerNo']" label-width="120px"
          input-width="140px">
          <template slot="append">
            <el-button type="primary" @click="searchData">查询</el-button>
          </template>
        </sys-common-search>
      </el-dialog>
    </div>
    `;
  return {
    props: {
      toOrderMaterialData: {
        type: Object,
        required: true
      },
      deptAndWork: {
        type: Array
      },
      type: {
        type: Number,
        default: 1
      }
    },
    data: function () {
      var search = {
        companyOrderId: '',
        orderId: '',
        factoryNo: '',
        customerNo: ''
      };
      return {
        loading: false,
        isShowToOrderForm: false,
        //订单明细信息
        orderInfoDetailList: [],
        user: null,
        search: _.cloneDeep(search),
        oldSearch: _.cloneDeep(search),
        pagination: {
          currentPage: 1,
          totalCount: 0,
          pageSize: 10
        }
      }
    },
    created: function () {
      var self = this;
      this.setUser().then(function () {
        self.setOrderInfoDetailList(true);
      });
    },
    methods: {
      searchData: function () {
        this.pagination.currentPage = 1;
        this.isShowToOrderForm = false;
        this.setOrderInfoDetailList(true);
      },
      pageChange: function () {
        this.setOrderInfoDetailList();
      },
      //获取订单物料信息
      setOrderInfoDetailList: function (isSearch) {
        if (!isSearch) {
          this.search = _.cloneDeep(this.oldSearch);
        }
        var search = this.search;
        var pagination = this.pagination;
        var currentPage = pagination.currentPage;
        var pageSize = pagination.pageSize;
        var start = (currentPage - 1) * pageSize;
        var materialData = this.toOrderMaterialData;
        var postData = {
          start: start,
          length: pageSize,
          isReadColor: 1,
          materialCode: materialData.materialCode
        }
        if (materialData.color === null || materialData.color.trim() === "") {
          postData.isReadColor = 2;
        }
        if (search.orderId !== '') {
          postData.orderId = search.orderId;
        }
        if (search.companyOrderId !== '') {
          postData.companyOrderId = search.companyOrderId;
        }
        if (search.factoryNo !== '') {
          postData.factoryNo = search.factoryNo;
        }
        if (search.customerNo !== '') {
          postData.customerNo = search.customerNo;
        }
        this.loading = true;
        var self = this;
        ajaxRequest("issueNote/getOrderInfoDetailByNotProduct", postData).then(function (data) {
          if (isSearch) {
            self.oldSearch = _.cloneDeep(self.search);
          }
          var dataList = data.returnData;
          self.orderInfoDetailList = dataList;
          self.pagination.totalCount = data.iTotalRecords;
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      },
      saveOut: function (rowData) {
        var toOrderMaterialData = this.toOrderMaterialData;
        var materialData = {
          materialName: toOrderMaterialData.materialName,
          color: toOrderMaterialData.color,
          providerName: toOrderMaterialData.providerName,
          model: toOrderMaterialData.modal,
          colorDescription: toOrderMaterialData.colorDescription,
          count: toOrderMaterialData.warehouseCount,
          materialCode: toOrderMaterialData.materialCode,
          unit: toOrderMaterialData.unit
        };
        if (rowData.feedCount === undefined || rowData.feedCount === 0) {
          this.$alert("投料数不能为0");
          return;
        }
        if (rowData.count === undefined || rowData.count === 0) {
          this.$alert("出库数量不能为0");
          return;
        }

        if (toOrderMaterialData.unit !== '付') {
          if (rowData.selfOutFuCount === undefined || rowData.selfOutFuCount === 0) {
            this.$alert("交接数量不能为0");
            return;
          }
        }
        var type = this.type;
        var postData = {
          materialData: JSON.stringify(materialData),
          orderData: JSON.stringify(rowData),
          outCount: rowData.count,
          selfOutFuCount: rowData.selfOutFuCount,
          type: type
        };
        if (type === 1) {
          var deptAndWork = this.deptAndWork;
          deptAndWork = ModuleUtil.getDeptWorkshop(deptAndWork);
          postData.deptId = deptAndWork.deptId;
          postData.workShopId = deptAndWork.workShopId;
          postData.userId = deptAndWork.pickingPeople;
        }

        this.loading = true;
        var self = this;
        ajaxRequest("warehouse/saveOutToOrder", postData).then(function (res) {
          self.loading = false;
          self.$alert(res, "信息");
          self.$emit("after");
        }).catch(function () {
          self.loading = false;
        });
      },
      //获取用户部门车间信息
      setUser: function () {
        var type = this.type;
        if (type === 1) {
          var deptAndWork = this.deptAndWork;
          deptAndWork = ModuleUtil.getDeptWorkshop(deptAndWork);
          var userId = deptAndWork.pickingPeople;

          this.loading = true;
          var self = this;
          return ajaxRequest("user/getUserByDeptAndWork", {
            userId: userId
          }).then(function (data) {
            self.user = data[0];
            self.loading = false;
            return data[0];
          }).catch(function () {
            self.loading = false;
            return null;
          });
        }
        return Promise.resolve(null);
      }
    },
    template: template,
    components: {
      SysCommonSearch: SysCommonSearch
    }
  }
}();
